package StructurePattern.Facade;

/**
 * @author: Themberfue
 * @date: 2025/8/12 11:09
 * @description:
 */
// 1. 子系统1：DVD播放器
class DvdPlayer {
    public void on() {
        System.out.println("DVD播放器已开启");
    }

    public void off() {
        System.out.println("DVD播放器已关闭");
    }

    public void play(String movie) {
        System.out.println("正在播放电影: " + movie);
    }

    public void stop() {
        System.out.println("DVD播放已停止");
    }
}

// 2. 子系统2：音响
class Amplifier {
    public void on() {
        System.out.println("音响已开启");
    }

    public void off() {
        System.out.println("音响已关闭");
    }

    public void setVolume(int level) {
        System.out.println("音响音量已设置为: " + level);
    }
}

// 3. 子系统3：投影仪
class Projector {
    public void on() {
        System.out.println("投影仪已开启");
    }

    public void off() {
        System.out.println("投影仪已关闭");
    }

    public void setInput(String input) {
        System.out.println("投影仪输入源已设置为: " + input);
    }

    public void zoomIn() {
        System.out.println("投影仪画面已放大");
    }
}

// 4. 子系统4：灯光
class Lights {
    public void dim(int percentage) {
        System.out.println("灯光已调暗至 " + percentage + "%");
    }

    public void on() {
        System.out.println("灯光已开启");
    }
}

// 5. 外观类：家庭影院外观
class HomeTheaterFacade {
    // 持有子系统的引用
    private DvdPlayer dvd;
    private Amplifier amp;
    private Projector projector;
    private Lights lights;

    // 构造函数初始化子系统
    public HomeTheaterFacade(DvdPlayer dvd, Amplifier amp,
                             Projector projector, Lights lights) {
        this.dvd = dvd;
        this.amp = amp;
        this.projector = projector;
        this.lights = lights;
    }

    // 简化的接口：观看电影
    public void watchMovie(String movie) {
        System.out.println("\n=== 准备观看电影 ===");
        lights.dim(10);         // 调暗灯光
        projector.on();         // 打开投影仪
        projector.setInput("DVD"); // 设置输入源
        projector.zoomIn();     // 放大画面
        amp.on();               // 打开音响
        amp.setVolume(8);       // 设置音量
        dvd.on();               // 打开DVD
        dvd.play(movie);        // 播放电影
    }

    // 简化的接口：结束观影
    public void endMovie() {
        System.out.println("\n=== 结束观影 ===");
        lights.on();            // 打开灯光
        dvd.stop();             // 停止播放
        dvd.off();              // 关闭DVD
        amp.off();              // 关闭音响
        projector.off();        // 关闭投影仪
    }
}

// 6. 客户端代码
public class FacadeExample {
    public static void main(String[] args) {
        // 创建子系统对象
        DvdPlayer dvd = new DvdPlayer();
        Amplifier amp = new Amplifier();
        Projector projector = new Projector();
        Lights lights = new Lights();

        // 创建外观对象
        HomeTheaterFacade homeTheater = new HomeTheaterFacade(dvd, amp, projector, lights);

        // 通过外观接口操作复杂系统
        homeTheater.watchMovie("星际穿越");

        // 观影结束
        homeTheater.endMovie();
    }
}

